package com.xfyr.mvpdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hjq.toast.ToastUtils;
import com.xfyr.mvpdemo.adapter.AdapterMainList;
import com.xfyr.mvpdemo.base.BaseMvpActivity;
import com.xfyr.mvpdemo.contract.MainContract;
import com.xfyr.mvpdemo.presenter.MainPresenterImpl;

import java.util.ArrayList;
import java.util.List;

import bean.NewsBean;
import butterknife.BindView;
import butterknife.OnClick;


public class MainActivity extends BaseMvpActivity<MainPresenterImpl> implements MainContract.View {

    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private boolean isOnKeyBacking;
    private Handler handler = new Handler();


    private AdapterMainList adapterMainList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mPresenter = new MainPresenterImpl(this);
        mPresenter.attachView(this);
        initRecyclerView();
    }

    private void initRecyclerView() {
        adapterMainList = new AdapterMainList(new ArrayList<>());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapterMainList);
        adapterMainList.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mPresenter.onItemClick((NewsBean) adapter.getData().get(position));
            }
        });
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                srl.setRefreshing(true);
                mPresenter.onRefresh();
            }
        });
    }


    /**
     * 监听返回--是否退出程序
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_BACK) {
            return false;
        }
        if (isOnKeyBacking) {
            handler.removeCallbacks(onBackTimeRunnable);
            // 退出
            AppExit();
            finish();
            return true;
        } else {
            isOnKeyBacking = true;
            ToastUtils.show("再按一次退出程序");
            handler.postDelayed(onBackTimeRunnable, 2000);
            return true;
        }
    }

    private Runnable onBackTimeRunnable = new Runnable() {
        @Override
        public void run() {
            isOnKeyBacking = false;
        }
    };

    /**
     * 退出应用程序
     */
    public void AppExit() {
        try {
            // 杀死该应用进程
            Process.killProcess(Process.myPid());
            System.exit(0);
        } catch (Exception e) {
        }
    }


    @Override
    public void showLoading() {
        showLoadingView(false);
    }

    @Override
    public void hideLoading() {
        hideLoadingView();
    }

    @Override
    public void onError(String errorCode, String errorMsg) {
        ToastUtils.show(errorMsg);
    }

    @Override
    public void onSuccess(List<NewsBean> newsBeans) {
        adapterMainList.setNewData(newsBeans);
        srl.setRefreshing(false);
    }

    @Override
    public String newsCount() {
        if (TextUtils.isEmpty(et.getText().toString())) {
            return "10";
        }
        return et.getText().toString();
    }

    @Override
    public void onItemSelect(NewsBean newsBean) {
        ToastUtils.show(newsBean.description);
    }

    @Override
    public void onListRefresh() {
        mPresenter.getNewsP("5baef83129decfa305bbc140b085d8a1");
    }


    @OnClick(R.id.btn)
    public void onViewClicked() {
        mPresenter.getNewsP("5baef83129decfa305bbc140b085d8a1");
    }
}
